package com.ikun.aimian.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ikun.aimian.annotation.AuthCheck;
import com.ikun.aimian.common.BaseResponse;
import com.ikun.aimian.common.DeleteRequest;
import com.ikun.aimian.common.ErrorCode;
import com.ikun.aimian.common.ResultUtils;
import com.ikun.aimian.constant.UserConstant;
import com.ikun.aimian.exception.ThrowUtils;
import com.ikun.aimian.model.dto.job.JobAddRequest;
import com.ikun.aimian.model.dto.job.JobEditRequest;
import com.ikun.aimian.model.dto.job.JobQueryRequest;
import com.ikun.aimian.model.entity.Job;
import com.ikun.aimian.model.vo.JobVO;
import com.ikun.aimian.service.JobService;
import com.ikun.aimian.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/job")
@Slf4j
public class JobController {

    @Resource
    private JobService jobService;

    @Resource
    private UserService userService;

    /**
     * 新增岗位（仅管理员可用）
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> addJob(@RequestBody JobAddRequest addRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(addRequest == null, ErrorCode.PARAMS_ERROR);
        Job job = new Job();
        BeanUtils.copyProperties(addRequest, job);
        jobService.validJob(job, true);
        boolean result = jobService.save(job);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "岗位发布失败");
        return ResultUtils.success(job.getId());
    }

    /**
     * 编辑岗位（仅管理员可用）
     */
    @PostMapping("/edit")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> editJob(@RequestBody JobEditRequest editRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(editRequest == null || editRequest.getId() == null || editRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        Job job = new Job();
        BeanUtils.copyProperties(editRequest, job);
        jobService.validJob(job, false);
        boolean result = jobService.updateById(job);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "岗位更新失败");
        return ResultUtils.success(true);
    }

    /**
     * 删除岗位（仅管理员可用）
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteJob(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() == null || deleteRequest.getId() <= 0) {
            throw new RuntimeException("参数错误");
        }
        boolean result = jobService.removeById(deleteRequest.getId());
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "删除岗位失败");
        return ResultUtils.success(true);
    }

    /**
     * 根据ID获取岗位详情（封装类）
     */
    @GetMapping("/get/vo")
    public BaseResponse<JobVO> getJobVOById(@RequestParam Long id, HttpServletRequest request) {
        ThrowUtils.throwIf(id == null || id <= 0, ErrorCode.PARAMS_ERROR);
        Job job = jobService.getById(id);
        ThrowUtils.throwIf(job == null, ErrorCode.NOT_FOUND_ERROR, "岗位不存在");
        JobVO vo = jobService.getJobVO(job, request);
        return ResultUtils.success(vo);
    }

    @PostMapping("/list/page")
    public BaseResponse<Page<JobVO>> listJobByPage(@RequestBody JobQueryRequest queryRequest, HttpServletRequest request) {
        long current = queryRequest.getCurrent();
        long size = queryRequest.getPageSize();
        Page<Job> jobPage = jobService.page( 
                new Page<>(current, size),
                jobService.getQueryWrapper(queryRequest)
        );
        Page<JobVO> voPage = jobService.getJobVOPage(jobPage, request);
        return ResultUtils.success(voPage);
    }
}
